package com.kyzh.core.adapters

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.gushenge.core.beans.PointDetail
import com.gushenge.core.impls.UserImpl
import com.gushenge.core.impls.WealImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import org.jetbrains.anko.toast

class PointDetailTabAdapter(val context: Context, val titles: ArrayList<String>, val type: Int) : PagerAdapter() {

    var p1=1
    var max1 = 1

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = View.inflate(context, R.layout.recyclerview,null)
        val root = view.findViewById<SmartRefreshLayout>(R.id.root)
        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)

        val beans = arrayListOf<PointDetail>()
        recyclerView.layoutManager = object :LinearLayoutManager(context){
            override fun canScrollHorizontally(): Boolean {
                return false
            }
        }
        p1=1
        max1=1
        val adapter = PointDetailAdapter(R.layout.point_detail_item, beans, type)
        recyclerView.adapter = adapter
        root.autoRefresh()
        root.setEnableAutoLoadMore(false)
        root.setOnRefreshListener {
            p1=1
            addData(root,beans,adapter,position)
        }

        root.setOnLoadMoreListener {
            if (p1>max1){
                context.toast(context.getString(R.string.noHaveMore))
                root.finishLoadMore()
            }else{
                addData(root,beans,adapter,position)
            }
        }

        container.addView(view)
        return view
    }

    private fun addData(root: SmartRefreshLayout, bean: ArrayList<PointDetail>, adapter: PointDetailAdapter, position: Int) {
        if (type == 1) {
            WealImpl.pointDetail(position, p1, object : ResultListener {
                override fun error() {
                    adapter.setEmptyView(R.layout.empty)
                    root.finishRefresh()
                    root.finishLoadMore()
                }

                override fun success(beans: Any, p: Int, max: Int) {
                    beans as ArrayList<PointDetail>
                    root.finishRefresh()
                    root.finishLoadMore()
                    if (p == 2) {
                        bean.clear()
                    }
                    bean.addAll(beans)
                    adapter.notifyDataSetChanged()
                    p1 = p
                    max1 = max
                }
            })
        } else {
            UserImpl.bill(position, p1, object : ResultListener {
                override fun error() {
                    adapter.setEmptyView(R.layout.empty)
                    root.finishRefresh()
                    root.finishLoadMore()
                }

                override fun success(beans: Any, p: Int, max: Int) {
                    beans as ArrayList<PointDetail>
                    root.finishRefresh()
                    root.finishLoadMore()
                    if (p == 2) {
                        bean.clear()
                    }
                    bean.addAll(beans)
                    adapter.notifyDataSetChanged()
                    p1 = p
                    max1 = max
                }
            })
        }
    }


    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view ==`object`
    }

    override fun getCount(): Int {
        return titles.size
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return titles[position]
    }
}